package com.budwk.app.sqfwV2.generator.patrolAndVisit;


import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.constants.CodeGeneratorConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

@IocBean
public class PatrolAndVisitExcelForAdminGenerator extends BaseExcelGenerator {

    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    private String templateName;


    public PatrolAndVisitExcelForAdminGenerator() {
    }

    public PatrolAndVisitExcelForAdminGenerator(String templateName) {
        this.templateName = templateName;
    }


    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {
        // 如果是后台admin调用, 则做以下处理
        if (StringUtils.isNotBlank(excelGenerateParam.getClientType()) && excelGenerateParam.getClientType().equals(CodeGeneratorConstants.CLIENT_TYPE_ADMIN)) {
            List<ExcelRowDataParam> dataList = excelGenerateParam.getDataList();

            for (ExcelRowDataParam excelRowDataParam : dataList) {
                if (!addRowDataCondition(excelRowDataParam, excelGenerateParam)) continue;

                CommonParam commonParam = excelRowDataParam.getCreateUserCommonParam();
                AbstractCustomerPdfCreateField customerPdfCreateField = excelRowDataParam.getAbstractCustomerPdfCreateField();
                Map<String, Object> rowDataMap = Maps.newHashMap();
                Class<? extends AbstractCustomerPdfCreateField> fieldDataClass = customerPdfCreateField.getClass();
                Field[] declaredFields = fieldDataClass.getDeclaredFields();
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    String fieldName = declaredField.getName();
                    Object fieldValue = getFieldValueByType(declaredField, customerPdfCreateField);
                    rowDataMap.put(fieldName, fieldValue);
                }
                customerPopularRowDataMapForAdmin(rowDataMap, excelRowDataParam);
                listMap.add(rowDataMap);
            }
        }
    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
        Map<String, Object> customerParamMap = (Map) rowDataParam.getCustomerParamMap();
        rowDataMap.put("community", customerParamMap.get("community").equals("直属") ? customerParamMap.get("county") : customerParamMap.get("community"));
        rowDataMap.put("publicityRecord", customerParamMap.get("publicityRecord"));
        rowDataMap.put("searchRecord", customerParamMap.get("searchRecord"));
        rowDataMap.put("peaceAndCare", customerParamMap.get("peaceAndCare"));
    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return PatrolAndVisitEnum.getFileName(Integer.parseInt(code));
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }


}
